<?php

namespace App\Http\Controllers\Platform;

use App\Http\Controllers\Controller;
use App\Http\Requests\Platform\AdminRequest;
use App\Models\Admin;
use App\Services\AdminService;
use App\Utils\ResultUtil;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    /**
     * 管理员列表
     * @param AdminService $adminService
     * @return array
     */
    public function index(AdminService $adminService)
    {
        $page = $adminService->page();
        return ResultUtil::data($page);
    }


    /**
     * 新增管理员
     * description
     * @param AdminRequest $request
     * @param AdminService $adminService
     * @return array
     * @throws \App\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function add(AdminRequest $request,AdminService $adminService)
    {
        $request->scene('add')->validate();
        $data=$request->validationData();
        $adminService->add($data);
        return ResultUtil::success();
    }



    /**
     * 修改管理员信息
     * @param AdminRequest $request
     * @param AdminService $adminService
     * @return array
     * @throws \App\Exceptions\ApiException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function edit(AdminRequest $request,AdminService $adminService)
    {
        $request->scene('edit')->validate();
        $data=$request->validationData();
        $adminService->edit($data);
        return ResultUtil::success();

    }

    /**
     * 删除
     * @param $id
     * @param AdminService $adminService
     * @return array
     * @throws \Exception
     */
    public function del(int $id,AdminService $adminService)
    {
        $adminService->del($id);
        return ResultUtil::success();
    }


    public function checkAuth(AdminService $adminService){
        $adminService->checkAuth(1,3);
    }


    public function login(AdminRequest $request,AdminService $adminService){
        $request->scene('login')->validate();
        $data=$request->validationData();
        $ip = $request->getClientIp();
        $token = $adminService->login($data['account'],$data['password'],$ip);
        return ResultUtil::data(['token'=>$token]);
    }
}
